package com.dianping.cat.consumer.storage.model.transform;
import com.dianping.cat.consumer.storage.model.IVisitor;
import com.dianping.cat.consumer.storage.model.entity.Domain;
import com.dianping.cat.consumer.storage.model.entity.Machine;
import com.dianping.cat.consumer.storage.model.entity.Operation;
import com.dianping.cat.consumer.storage.model.entity.Segment;
import com.dianping.cat.consumer.storage.model.entity.Sql;
import com.dianping.cat.consumer.storage.model.entity.StorageReport;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;

  public DefaultNativeParser(InputStream in) {
/*  18 */     this.m_linker = new DefaultLinker(true);




/*  23 */     this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;

/*  27 */   public static StorageReport parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }


  public static StorageReport parse(InputStream in) {
    DefaultNativeParser parser = new DefaultNativeParser(in);
    StorageReport storageReport = new StorageReport();

    try {
/*  35 */       storageReport.accept(parser);
    } catch (RuntimeException e) {
/*  37 */       if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {

/*  40 */         throw e;
      }
    }

/*  44 */     parser.m_linker.finish();
/*  45 */     return storageReport;
  }



  public void visitDomain(Domain domain) {
    byte tag;
/*  52 */     while ((tag = readTag()) != -1) {
/*  53 */       visitDomainChildren(domain, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitDomainChildren(Domain domain, int _field, int _type) {
/*  58 */     switch (_field) {
      case 1:
/*  60 */         domain.setId(readString());
        break;
      case 33:
/*  63 */         if (_type == 1) {
/*  64 */           Operation operation = new Operation();

/*  66 */           visitOperation(operation);
/*  67 */           this.m_linker.onOperation(domain, operation); break;
/*  68 */         }  if (_type == 2) {
/*  69 */           for (int i = readInt(); i > 0; i--) {
/*  70 */             Operation operation = new Operation();

/*  72 */             visitOperation(operation);
/*  73 */             this.m_linker.onOperation(domain, operation);
          }
        }
        break;
      case 34:
        if (_type == 1) {
/*  79 */           Sql sql = new Sql();

/*  81 */           visitSql(sql);
/*  82 */           this.m_linker.onSql(domain, sql); break;
/*  83 */         }  if (_type == 2) {
/*  84 */           for (int i = readInt(); i > 0; i--) {
/*  85 */             Sql sql = new Sql();

/*  87 */             visitSql(sql);
/*  88 */             this.m_linker.onSql(domain, sql);
          }
        }
        break;
    }
  }



  public void visitMachine(Machine machine) {
    byte tag;
/*  99 */     while ((tag = readTag()) != -1) {
/* 100 */       visitMachineChildren(machine, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitMachineChildren(Machine machine, int _field, int _type) {
/* 105 */     switch (_field) {
      case 1:
/* 107 */         machine.setId(readString());
        break;
      case 33:
/* 110 */         if (_type == 1) {
/* 111 */           Domain domain = new Domain();

/* 113 */           visitDomain(domain);
/* 114 */           this.m_linker.onDomain(machine, domain); break;
/* 115 */         }  if (_type == 2) {
/* 116 */           for (int i = readInt(); i > 0; i--) {
/* 117 */             Domain domain = new Domain();

/* 119 */             visitDomain(domain);
/* 120 */             this.m_linker.onDomain(machine, domain);
          }
        }
        break;
    }
  }



  public void visitOperation(Operation operation) {
    byte tag;
/* 131 */     while ((tag = readTag()) != -1) {
/* 132 */       visitOperationChildren(operation, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitOperationChildren(Operation operation, int _field, int _type) {
/* 137 */     switch (_field) {
      case 1:
/* 139 */         operation.setId(readString());
        break;
      case 2:
/* 142 */         operation.setCount(readLong());
        break;
      case 3:
/* 145 */         operation.setAvg(readDouble());
        break;
      case 4:
/* 148 */         operation.setSum(readDouble());
        break;
      case 5:
/* 151 */         operation.setError(readLong());
        break;
      case 6:
/* 154 */         operation.setLongCount(readLong());
        break;
      case 33:
/* 157 */         if (_type == 1) {
/* 158 */           Segment segment = new Segment();

/* 160 */           visitSegment(segment);
/* 161 */           this.m_linker.onSegment(operation, segment); break;
/* 162 */         }  if (_type == 2) {
/* 163 */           for (int i = readInt(); i > 0; i--) {
/* 164 */             Segment segment = new Segment();

/* 166 */             visitSegment(segment);
/* 167 */             this.m_linker.onSegment(operation, segment);
          }
        }
        break;
    }
  }



  public void visitSegment(Segment segment) {
    byte tag;
/* 178 */     while ((tag = readTag()) != -1) {
/* 179 */       visitSegmentChildren(segment, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitSegmentChildren(Segment segment, int _field, int _type) {
/* 184 */     switch (_field) {
      case 1:
/* 186 */         segment.setId(readInt());
        break;
      case 2:
/* 189 */         segment.setCount(readLong());
        break;
      case 3:
/* 192 */         segment.setAvg(readDouble());
        break;
      case 4:
/* 195 */         segment.setSum(readDouble());
        break;
      case 5:
/* 198 */         segment.setError(readLong());
        break;
      case 6:
/* 201 */         segment.setLongCount(readLong());
        break;
    }
  }



  public void visitSql(Sql sql) {
    byte tag;
/* 210 */     while ((tag = readTag()) != -1) {
/* 211 */       visitSqlChildren(sql, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitSqlChildren(Sql sql, int _field, int _type) {
/* 216 */     switch (_field) {
      case 1:
/* 218 */         sql.setId(readString());
        break;
      case 2:
/* 221 */         sql.setStatement(readString());
        break;
      case 3:
/* 224 */         sql.setCount(readInt());
        break;
    }
  }



  public void visitStorageReport(StorageReport storageReport) {
    byte tag;
/* 233 */     if ((tag = readTag()) != -4) {
/* 234 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }

/* 237 */     while ((tag = readTag()) != -1) {
/* 238 */       visitStorageReportChildren(storageReport, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitStorageReportChildren(StorageReport storageReport, int _field, int _type) {
/* 243 */     switch (_field) {
      case 1:
/* 245 */         storageReport.setId(readString());
        break;
      case 2:
/* 248 */         storageReport.setName(readString());
        break;
      case 3:
/* 251 */         storageReport.setType(readString());
        break;
      case 4:
/* 254 */         storageReport.setStartTime(readDate());
        break;
      case 5:
/* 257 */         storageReport.setEndTime(readDate());
        break;
      case 6:
/* 260 */         if (_type == 1) {
/* 261 */           storageReport.addId(readString()); break;
/* 262 */         }  if (_type == 2) {
/* 263 */           for (int i = readInt(); i > 0; i--) {
/* 264 */             storageReport.addId(readString());
          }
        }
        break;
      case 7:
/* 269 */         if (_type == 1) {
/* 270 */           storageReport.addIp(readString()); break;
/* 271 */         }  if (_type == 2) {
/* 272 */           for (int i = readInt(); i > 0; i--) {
/* 273 */             storageReport.addIp(readString());
          }
        }
        break;
      case 8:
/* 278 */         if (_type == 1) {
/* 279 */           storageReport.addOp(readString()); break;
/* 280 */         }  if (_type == 2) {
/* 281 */           for (int i = readInt(); i > 0; i--) {
/* 282 */             storageReport.addOp(readString());
          }
        }
        break;
      case 33:
/* 287 */         if (_type == 1) {
/* 288 */           Machine machine = new Machine();

/* 290 */           visitMachine(machine);
/* 291 */           this.m_linker.onMachine(storageReport, machine); break;
/* 292 */         }  if (_type == 2) {
/* 293 */           for (int i = readInt(); i > 0; i--) {
/* 294 */             Machine machine = new Machine();

/* 296 */             visitMachine(machine);
/* 297 */             this.m_linker.onMachine(storageReport, machine);
          }
        }
        break;
    }
  }

  private Date readDate() {
    try {
/* 306 */       return new Date(readVarint(64));
/* 307 */     } catch (IOException e) {
/* 308 */       throw new RuntimeException(e);
    }
  }

  private double readDouble() {
    try {
/* 314 */       return Double.longBitsToDouble(readVarint(64));
/* 315 */     } catch (IOException e) {
/* 316 */       throw new RuntimeException(e);
    }
  }

  private int readInt() {
    try {
/* 322 */       return (int)readVarint(32);
/* 323 */     } catch (IOException e) {
/* 324 */       throw new RuntimeException(e);
    }
  }

  private long readLong() {
    try {
/* 330 */       return readVarint(64);
/* 331 */     } catch (IOException e) {
/* 332 */       throw new RuntimeException(e);
    }
  }

  private String readString() {
    try {
/* 338 */       return this.m_in.readUTF();
/* 339 */     } catch (IOException e) {
/* 340 */       throw new RuntimeException(e);
    }
  }

  private byte readTag() {
    try {
/* 346 */       return this.m_in.readByte();
/* 347 */     } catch (IOException e) {
/* 348 */       throw new RuntimeException(e);
    }
  }

  protected long readVarint(int length) throws IOException {
/* 353 */     int shift = 0;
/* 354 */     long result = 0L;

/* 356 */     while (shift < length) {
/* 357 */       byte b = this.m_in.readByte();
/* 358 */       result |= (b & 0x7F) << shift;
/* 359 */       if ((b & 0x80) == 0) {
/* 360 */         return result;
      }
/* 362 */       shift += 7;
    }

/* 365 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/storage/model/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */